#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Joycon 修复脚本
"""

import subprocess
import sys

def run_cmd(cmd):
    """运行命令"""
    try:
        result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
        return result.returncode == 0, result.stdout
    except:
        return False, ""

def main():
    print("Joycon 修复脚本")
    print("="*30)
    
    # 1. 重启蓝牙服务
    print("1. 重启蓝牙服务...")
    success, _ = run_cmd("sudo systemctl restart bluetooth")
    if success:
        print("   ✅ 蓝牙服务重启成功")
    else:
        print("   ❌ 蓝牙服务重启失败")
    
    # 2. 加载内核模块
    print("2. 加载内核模块...")
    success, _ = run_cmd("sudo modprobe hid-nintendo")
    if success:
        print("   ✅ hid-nintendo 模块加载成功")
    else:
        print("   ❌ hid-nintendo 模块加载失败")
    
    success, _ = run_cmd("sudo modprobe hid-generic")
    if success:
        print("   ✅ hid-generic 模块加载成功")
    else:
        print("   ❌ hid-generic 模块加载失败")
    
    # 3. 检查设备
    print("3. 检查设备...")
    success, stdout = run_cmd("bluetoothctl devices")
    if success:
        joycon_found = False
        for line in stdout.split('\n'):
            if 'joy-con' in line.lower():
                print(f"   ✅ 发现 Joycon: {line.strip()}")
                joycon_found = True
        if not joycon_found:
            print("   ❌ 未发现 Joycon 设备")
    
    print("\n修复完成！")
    print("如果问题仍然存在，请:")
    print("1. 重新配对手柄")
    print("2. 重启系统")
    print("3. 检查手柄电池")

if __name__ == "__main__":
    main() 